//
//  WWTools.m
//  SportsWin
//
//  Created by 于洪东 on 2019/5/18.
//  Copyright © 2019 douniwan. All rights reserved.
//

#import "WWTools.h"

@implementation WWTools

+ (void)removeValueforKey:(NSString *)key {
    if ([WWTools readValueForKey:key]) {
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:key];
    }
}

+ (void)writeValue:(id)value forKey:(NSString *)key {
    [[NSUserDefaults standardUserDefaults]  setObject:[NSKeyedArchiver archivedDataWithRootObject:value] forKey:key];
}

+ (id)readValueForKey:(NSString *)key {
    id obj = [[NSUserDefaults standardUserDefaults] objectForKey:key];
    if (!obj) {
        return nil;
    }
    return [NSKeyedUnarchiver unarchiveObjectWithData:obj];
}

+ (BOOL)isPhoneX {
    BOOL iPhoneX = NO;
    if (@available(iOS 11.0, *)) {
        UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window];
        if (mainWindow.safeAreaInsets.bottom > 0.0) {
            iPhoneX = YES;
        }
    }
    return iPhoneX;
}

+ (void)calulateImageFileSize:(UIImage *)image {
    NSData *data = UIImageJPEGRepresentation(image, 1);
    [self calulateImageFileSizeWithData:data];
}

+ (void)calulateImageFileSizeWithData:(NSData *)data {
    double dataLength = [data length] * 1.0;
    NSArray *typeArray = @[@"bytes",@"KB",@"MB",@"GB",@"TB",@"PB", @"EB",@"ZB",@"YB"];
    NSInteger index = 0;
    while (dataLength > 1024) {
        dataLength /= 1024.0;
        index ++;
    }
    NSLog(@"image = %.3f %@",dataLength,typeArray[index]);
}


+ (UIImage *)makeImageWithView:(UIView *)view withSize:(CGSize)size{
    // 下面方法，第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果，需要传NO，否则传YES。第三个参数就是屏幕密度了，关键就是第三个参数 [UIScreen mainScreen].scale。
    UIGraphicsBeginImageContextWithOptions(size, YES, [UIScreen mainScreen].scale);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

+(UIImage *)attachPlaceHolderImage{
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 150*ScaleX, 200*ScaleX)];
    backView.backgroundColor = NTLightGrayColor;
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 150*ScaleX, 200*ScaleX)];
    label.text = @"图片加载中...";
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:16*ScaleX];
    label.textColor = [UIColor whiteColor];
    [backView addSubview:label];

    return [self makeImageWithView:backView withSize:CGSizeMake(150*ScaleX, 200*ScaleX)];
}

+ (void)xDic:(NSMutableDictionary *)xDic setObj:(id)i forKey:(NSString*)key
{
    if (i)
    {
        xDic[key] = i;
    }
}
+ (void)xArray:(NSMutableArray *)xArray addObject:(id)i
{
    if (i)
    {
        [xArray addObject:i];
    }
}

+ (NSString *)stringForValue:(id)value
{
    if (value)
    {
        NSString *str = [NSString stringWithFormat:@"%@",value];
        if ([str isEqualToString:@"<null>"])
        {
            return @"";
        }
        else
        {
            return str;
        }
    }
    else
    {
        return @"";
    }
}


+(BOOL)isInVersionCheck{
    return YES;
}






@end
